package cn.javabb.admin.controller.biz;

import cn.javabb.common.model.ResponseModel;
import cn.javabb.common.utils.ListUtils;
import cn.javabb.core.base.BaseController;
import cn.javabb.core.entity.biz.SitePageDO;
import cn.javabb.core.service.biz.SitePageService;
import com.github.pagehelper.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/admin/sitePage")
public class SitePageAdminController extends BaseController {

    @Autowired
    SitePageService sitePageService;

    /**
     * @author QINB
     * @param model
     * @return
     */
    @GetMapping("/list")
    public String toListPage(Model model) {
        return "admin/biz/sitePage/list";
    }
    /**
     * @author QINB
     * @param model
     * @return
     */
    @GetMapping("/save")
    public String toSavePage(Model model) {
        return "admin/biz/sitePage/sitePageSave";
    }

    /**
     * 查询角色列表
     */
    @ResponseBody
    @GetMapping("/")
    public Object list(String keyword) {
        String page = getPage();
        String size = getSize();
        Page p = sitePageService.list(keyword,page, size);
        return ResponseModel.table(p);
    }

    @ResponseBody
    @PostMapping("/")
    public Object saveOrUpdate(SitePageDO pageDO){
        int n = 0;
        if(null == pageDO.getPageId()){
            if(null == pageDO.getState()){pageDO.setState(1);}
            n = sitePageService.insertSelective(pageDO);
        }else{
            n = sitePageService.updateSelective(pageDO);
        }
        if(n>0){
            return ResponseModel.Success("保存成功");
        }
        return ResponseModel.Failure("保存失败");

    }
    /**
     * 删除
     * @param pageId
     * @return
     */
    @ResponseBody
    @DeleteMapping("/{pageId}")
    public Object delete(@PathVariable String pageId){
        if(StringUtils.isBlank(pageId)){
            return ResponseModel.Failure("参数错误");
        }
        List<String> idList = ListUtils.stringToListString(pageId);
        int num = sitePageService.deleteByIds(SitePageDO.class, "pageId", idList);
        if(num>0){
            return ResponseModel.Success("删除成功");
        }
        return ResponseModel.Failure("删除失败");
    }

    /**
     * 更新状态
     * @param pageId
     * @param state
     * @return
     */
    @ResponseBody
    @PostMapping("/updateState")
    public Object updateState(Integer pageId,Integer state){
        if(sitePageService.updateState(pageId,state)){
            return ResponseModel.Success("状态更新成功");
        }
        return ResponseModel.Failure("状态更新失败");
    }
}
